<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>diag</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : April 1993</div>
    <p>
      <b>diag</b> -  diagonal including or extracting</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[y]=diag(vm, [k])  </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>vm</b>
        </tt>: vector or matrix (full or sparse storage)</li>
      <li>
        <tt>
          <b>k</b>
        </tt>: integer (default value 0)</li>
      <li>
        <tt>
          <b>y</b>
        </tt>: vector or matrix</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
    for <tt>
        <b>vm</b>
      </tt> a (row or column) n-vector <tt>
        <b>diag(vm)</b>
      </tt> returns a diagonal
    matrix with entries of <tt>
        <b>vm</b>
      </tt> along the main diagonal.</p>
    <p>
      <tt>
        <b>diag(vm,k)</b>
      </tt> is a  <tt>
        <b>(n+abs(k))x(n+abs(k))</b>
      </tt> matrix with the entries 
    of <tt>
        <b>vm</b>
      </tt>  along the kth diagonal.  <tt>
        <b>k=0</b>
      </tt>  is the main diagonal 
    <tt>
        <b>k&gt;0</b>
      </tt>  is for upper diagonals and <tt>
        <b>k&lt;0</b>
      </tt> for lower
    diagonals.</p>
    <p>
    For a matrix <tt>
        <b>vm</b>
      </tt>, <tt>
        <b>diag(vm,k)</b>
      </tt> is the column vector made of entries
    of the kth diagonal of <tt>
        <b>vm</b>
      </tt>.  <tt>
        <b>diag(vm)</b>
      </tt> is the main diagonal of <tt>
        <b>vm</b>
      </tt>.
    <tt>
        <b>diag(diag(x))</b>
      </tt>  is a diagonal matrix.</p>
    <p>
    If  <tt>
        <b>vm</b>
      </tt> is a sparse matrix <tt>
        <b>diag(vm,k)</b>
      </tt>  returns a
    sparse  matrix.</p>
    <p>
    To construct a diagonal linear system, use <tt>
        <b>sysdiag</b>
      </tt>.</p>
    <p>
    Note that <tt>
        <b>eye(A).*A</b>
      </tt> returns a diagonal matrix made with the
    diagonal entries of <tt>
        <b>A</b>
      </tt>. This is valid for any matrix (constant,
    polynomial, rational, state-space linear system,...).</p>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>

diag([1,2])

A=[1,2;3,4];
diag(A)  // main diagonal
diag(A,1) 

diag(sparse(1:10))  // sparse diagonal matrix

// form a tridiagonal matrix of size 2*m+1
m=5;diag(-m:m) +  diag(ones(2*m,1),1) +diag(ones(2*m,1),-1)
 
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="sysdiag.htm">
        <tt>
          <b>sysdiag</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="sparse.htm">
        <tt>
          <b>sparse</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
  </body>
</html>
